package pers.jing.android.helper.page

import androidhelper.composeapp.generated.resources.Res
import androidhelper.composeapp.generated.resources.home
import androidhelper.composeapp.generated.resources.refresh
import androidhelper.composeapp.generated.resources.settings
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Home
import androidx.compose.material.icons.rounded.Refresh
import androidx.compose.material.icons.rounded.Settings
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import org.jetbrains.compose.resources.stringResource
import org.koin.compose.viewmodel.koinViewModel
import pers.jing.android.helper.widget.normalAppBarColors


/**
 * 尝试使用下拉刷新, 该组件未生效
 * 设备列表页面
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DeviceListPage(viewModel: DeviceListViewModel = koinViewModel<DeviceListViewModel>(),
                   onClickSettings: () -> Unit,
                   onClickDevice:(String) -> Unit) {
    Scaffold(
        topBar = {
            TopAppBar(
                title = {
                    Text(text = stringResource(Res.string.home))
                },
                navigationIcon = {
                    IconButton(
                        onClick = {
                            //设备
                        },
                    ) {
                        Icon(
                            imageVector = Icons.Rounded.Home,
                            contentDescription = stringResource(Res.string.home)
                        )
                    }
                },
                actions = {
                    IconButton(
                        onClick = {
                            viewModel.loadDevices()
                        }
                    ) {
                        Icon(
                            imageVector = Icons.Rounded.Refresh,
                            contentDescription = stringResource(Res.string.refresh)
                        )
                    }
                    IconButton(
                        onClick = {
                            onClickSettings()
                        }
                    ) {
                        Icon(
                            imageVector = Icons.Rounded.Settings,
                            contentDescription = stringResource(Res.string.settings)
                        )
                    }
                },
                colors = TopAppBarDefaults.normalAppBarColors()
            )
        }
    ) { paddings ->
        Column(
            modifier = Modifier.fillMaxSize().padding(paddings)
        ) {
            val adbVersion = viewModel.adbExists.collectAsState()
            if (!adbVersion.value) {
                //未安装, 引导安装
                Text(
                    text = "尚未安装Android SDK, 请前往设置进行安装",
                    fontSize = 17.sp,
                    color = MaterialTheme.colorScheme.primary,
                    modifier = Modifier.fillMaxWidth()
                        .padding(top = 16.dp)
                        .padding(horizontal = 16.dp)
                )
            } else {
                //显示设备列表
                val deviceList = viewModel.getStateFlowDevices().collectAsState()
                LazyColumn(
                    modifier = Modifier.fillMaxSize().padding(horizontal = 16.dp)
                ) {
                    items(deviceList.value) {
                        ElevatedCard(
                            modifier = Modifier.fillMaxWidth()
                                .padding(top = 10.dp)
                                .clickable {
                                    //打开应用列表
                                    onClickDevice(it.serialId)
                                }
                        ) {
                            Row(
                                modifier = Modifier.padding(horizontal = 16.dp)
                                    .padding(top = 10.dp)
                            ) {
                                Text(
                                    text = "设备名称: ${it.getDeviceName()}",
                                    color = MaterialTheme.colorScheme.primary,
                                    modifier = Modifier.weight(1.0f)
                                )

                                Text(
                                    text = "设备品牌: ${it.getDeviceBrand()}",
                                    modifier = Modifier.weight(1.0f)
                                        .padding(start = 10.dp)
                                )

                            }
                            Row(
                                modifier = Modifier.padding(horizontal = 16.dp)
                                    .padding(vertical = 10.dp)
                            ) {
                                Text(
                                    text = "设备型号: ${it.getDeviceModel()}",
                                    modifier = Modifier.weight(1.0f)
                                )

                                Text(
                                    text = "Android系统版本: ${it.getAndroidOSVersion()}",
                                    modifier = Modifier.weight(1.0f)
                                        .padding(start = 10.dp)
                                )
                            }
                        }
                    }
                }
            }

        }
    }

}